home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / imagemap / imap_csim.y < prev    next >
Encoding:
Lex Description  |  2002-12-30  |  6.4 KB  |  315 lines

  1. %{
  2. /*
  3.  * This is a plug-in for the GIMP.
  4.  *
  5.  * Generates clickable image maps.
  6.  *
  7.  * Copyright (C) 1998-1999 Maurits Rijk  lpeek.mrijk@consunet.nl
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22.  *
  23.  */
  24.  
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. #include "gtk/gtk.h"
  29.  
  30. #include "imap_circle.h"
  31. #include "imap_file.h"
  32. #include "imap_main.h"
  33. #include "imap_polygon.h"
  34. #include "imap_rectangle.h"
  35. #include "imap_string.h"
  36.  
  37. extern int csim_lex();
  38. extern int csim_restart();
  39. static void csim_error(char* s);
  40.  
  41. static enum {UNDEFINED, RECTANGLE, CIRCLE, POLYGON} current_type;
  42. static Object_t *current_object;
  43. static MapInfo_t *_map_info;
  44.  
  45. %}
  46.  
  47. %union {
  48.    int val;
  49.    double value;
  50.    char id[256];
  51. }
  52.  
  53. %token<val> IMG SRC WIDTH HEIGHT BORDER USEMAP
  54. %token<val> START_MAP END_MAP NAME AREA SHAPE COORDS ALT HREF NOHREF
  55. %token<val> TARGET ONMOUSEOVER ONMOUSEOUT ONFOCUS ONBLUR
  56. %token<val> AUTHOR DESCRIPTION BEGIN_COMMENT END_COMMENT
  57. %token<value> FLOAT
  58. %token<id> STRING
  59.  
  60. %%
  61.  
  62. csim_file    : image start_map comment_lines area_list end_map
  63.         ;
  64.  
  65. image        : '<' IMG SRC '=' STRING image_tags xhtml_close
  66.         {
  67.            g_strreplace(&_map_info->image_name, $5);
  68.         }
  69.         ;
  70.  
  71. image_tags    : /* Empty */
  72.         | image_tags image_tag
  73.         ;
  74.  
  75. image_tag    : image_width
  76.         | image_height
  77.         | BORDER '=' FLOAT {}
  78.         | USEMAP '=' STRING {}
  79.         | ALT '=' STRING {}
  80.         ;
  81.  
  82. image_width    : WIDTH '=' FLOAT 
  83.         {
  84.            _map_info->old_image_width = (gint) $3;
  85.         }
  86.         ;
  87.  
  88. image_height    : HEIGHT '=' FLOAT 
  89.         {
  90.            _map_info->old_image_height = (gint) $3;
  91.         }
  92.         ;
  93.  
  94. start_map    : '<' START_MAP NAME '=' STRING '>'
  95.         {
  96.            g_strreplace(&_map_info->title, $5);
  97.         }
  98.         ;
  99.  
  100. comment_lines    : /* empty */
  101.         | comment_lines comment_line
  102.         ;
  103.  
  104. comment_line    : author_line
  105.         | description_line
  106.         | real_comment
  107.         ;
  108.  
  109. real_comment    : BEGIN_COMMENT STRING END_COMMENT
  110.         {
  111.         }
  112.         ;
  113.  
  114. author_line    : AUTHOR STRING END_COMMENT
  115.         {
  116.            g_strreplace(&_map_info->author, $2);
  117.  
  118.         }
  119.         ;
  120.  
  121. description_line: DESCRIPTION STRING END_COMMENT
  122.         {
  123.            gchar *description;
  124.  
  125.            description = g_strconcat(_map_info->description, $2, "\n", 
  126.                          NULL);
  127.            g_strreplace(&_map_info->description, description);
  128.         }
  129.         ;
  130.  
  131. area_list    : /* empty */
  132.         | area_list area
  133.         ;
  134.  
  135. area        : '<' AREA tag_list xhtml_close
  136.         {
  137.            if (current_type != UNDEFINED)
  138.               add_shape(current_object);
  139.         }
  140.         ;
  141.  
  142. xhtml_close    : '>'
  143.         | '/' '>' 
  144.         ;
  145.  
  146. tag_list    : /* Empty */
  147.         | tag_list tag
  148.         ;
  149.  
  150. tag        : shape_tag
  151.         | coords_tag
  152.         | href_tag
  153.         | nohref_tag
  154.         | alt_tag
  155.         | target_tag
  156.         | onmouseover_tag
  157.         | onmouseout_tag
  158.         | onfocus_tag
  159.         | onblur_tag
  160.         ;
  161.  
  162. shape_tag    : SHAPE '=' STRING
  163.         {
  164.            if (!g_strcasecmp($3, "RECT")) {
  165.               current_object = create_rectangle(0, 0, 0, 0);
  166.               current_type = RECTANGLE;
  167.            } else if (!g_strcasecmp($3, "CIRCLE")) {
  168.               current_object = create_circle(0, 0, 0);
  169.               current_type = CIRCLE;
  170.            } else if (!g_strcasecmp($3, "POLY")) {
  171.               current_object = create_polygon(NULL);
  172.               current_type = POLYGON;
  173.            } else if (!g_strcasecmp($3, "DEFAULT")) {
  174.               current_type = UNDEFINED;
  175.            }
  176.         }
  177.         ;
  178.  
  179. coords_tag    : COORDS '=' STRING
  180.         {
  181.            char *p;
  182.            if (current_type == RECTANGLE) {
  183.               Rectangle_t *rectangle;
  184.  
  185.               rectangle = ObjectToRectangle(current_object);
  186.               p = strtok($3, ",");
  187.               rectangle->x = atoi(p);
  188.               p = strtok(NULL, ",");
  189.               rectangle->y = atoi(p);
  190.               p = strtok(NULL, ",");
  191.               rectangle->width = atoi(p) - rectangle->x;
  192.               p = strtok(NULL, ",");
  193.               rectangle->height = atoi(p) - rectangle->y;
  194.            } else if (current_type == CIRCLE) {
  195.               Circle_t *circle;
  196.  
  197.               circle = ObjectToCircle(current_object);
  198.               p = strtok($3, ",");
  199.               circle->x = atoi(p);
  200.               p = strtok(NULL, ",");
  201.               circle->y = atoi(p);
  202.               p = strtok(NULL, ",");
  203.               circle->r = atoi(p);
  204.            } else if (current_type == POLYGON) {
  205.               Polygon_t *polygon = ObjectToPolygon(current_object);
  206.               GList *points;
  207.               GdkPoint *point, *first;
  208.               gint x, y;
  209.  
  210.               p = strtok($3, ",");
  211.               x = atoi(p);
  212.               p = strtok(NULL, ",");
  213.               y = atoi(p);
  214.               point = new_point(x, y);
  215.               points = g_list_append(NULL, (gpointer) point);
  216.  
  217.               while(1) {
  218.              p = strtok(NULL, ",");
  219.              if (!p)
  220.                 break;
  221.              x = atoi(p);
  222.              p = strtok(NULL, ",");
  223.              y = atoi(p);
  224.              point = new_point(x, y);
  225.              g_list_append(points, (gpointer) point);
  226.               }
  227.               /* Remove last point if duplicate */
  228.               first = (GdkPoint*) points->data;
  229.               polygon->points = points;
  230.               if (first->x == point->x && first->y == point->y)
  231.              polygon_remove_last_point(polygon);
  232.               polygon->points = points;
  233.            }
  234.         }
  235.         ;
  236.  
  237. href_tag    : HREF '=' STRING
  238.         {
  239.            if (current_type == UNDEFINED) {
  240.               g_strreplace(&_map_info->default_url, $3);
  241.            } else {
  242.               object_set_url(current_object, $3);
  243.            }
  244.         }
  245.         ;
  246.  
  247. nohref_tag    : NOHREF
  248.         {
  249.         }
  250.         ;
  251.  
  252. alt_tag        : ALT '=' STRING
  253.         {
  254.            object_set_comment(current_object, $3);
  255.         }
  256.         ;
  257.  
  258. target_tag    : TARGET '=' STRING
  259.         {
  260.            object_set_target(current_object, $3);
  261.         }
  262.         ;
  263.  
  264. onmouseover_tag    : ONMOUSEOVER '=' STRING
  265.         {
  266.            object_set_mouse_over(current_object, $3);
  267.         }
  268.         ;
  269.  
  270. onmouseout_tag    : ONMOUSEOUT '=' STRING
  271.         {
  272.            object_set_mouse_out(current_object, $3);
  273.         }
  274.         ;
  275.  
  276. onfocus_tag    : ONFOCUS '=' STRING
  277.         {
  278.            object_set_focus(current_object, $3);
  279.         }
  280.         ;
  281.  
  282. onblur_tag    : ONBLUR '=' STRING
  283.         {
  284.            object_set_blur(current_object, $3);
  285.         }
  286.         ;
  287.  
  288. end_map        : '<' END_MAP '>'
  289.         ;
  290.  
  291. %%
  292.  
  293. static void 
  294. csim_error(char* s)
  295. {
  296.    extern FILE *csim_in;
  297.    csim_restart(csim_in);
  298. }
  299.  
  300. gboolean
  301. load_csim(const char* filename)
  302. {
  303.    gboolean status;
  304.    extern FILE *csim_in;
  305.    csim_in = fopen(filename, "r");
  306.    if (csim_in) {
  307.       _map_info = get_map_info();
  308.       status = !csim_parse();
  309.       fclose(csim_in);
  310.    } else {
  311.       status = FALSE;
  312.    }
  313.    return status;
  314. }
  315.